package com.tools.common.thread;

import com.tools.common.object.Note;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 时间值和它的单位的包装类
 * */
@Note("时间值和它的单位的包装类")
public final class TimePair {

    @Note("源时间值")
    private final long sourceTime;

    @Note("源时间单位")
    private final Timeunit sourceTimeunit;


    public TimePair(long sourceTime, Timeunit sourceTimeunit) {
        if(sourceTimeunit == null) throw new IllegalArgumentException("源时间单位不能为 null");
        this.sourceTime = sourceTime;
        this.sourceTimeunit = sourceTimeunit;
    }

    public static TimePair of(long sourceTime, Timeunit sourceTimeunit) {
        return new TimePair(sourceTime, sourceTimeunit);
    }

    @Note("解析字符串构建 TimePair 实例，解析出错不抛异常，只会返回 null")
    public static TimePair build(String timeText) {
        if(timeText == null) throw new NullPointerException("无法通过 null 构建具体的 TimePair 对象");
        int length = timeText.length();
        if(length < 2) throw new IllegalArgumentException("无法通过 " + timeText + " 构建具体的 TimePair 对象");
        return private_build(length, timeText);
    }

    @Note("解析字符串构建 TimePair 实例，解析出错会抛异常")
    public static TimePair buildSafe(String timeText) {
        if(timeText == null) return null;
        int length = timeText.length();
        if(length < 2) return null;
        return private_build(length, timeText);
    }

    @Note("只有大于 0 的时间才是有效时间")
    public boolean isEffectiveTime() {
        return this.sourceTime > 0;
    }

    @Note("转为 java.util.concurrent 包下的时间单位对象")
    public TimeUnit asTimeUnit() {
        return this.sourceTimeunit.asTimeUnit();
    }

    public long getSourceTime() {
        return sourceTime;
    }

    public Timeunit getSourceTimeunit() {
        return sourceTimeunit;
    }

    public long toNano() {
        return Timeunit.toNano(sourceTime, sourceTimeunit);
    }

    public long toMicro() {
        return Timeunit.toMicro(sourceTime, sourceTimeunit);
    }

    public long toMill() {
        return Timeunit.toMill(sourceTime, sourceTimeunit);
    }

    public long toSecond() {
        return Timeunit.toSecond(sourceTime, sourceTimeunit);
    }

    public long toMini() {
        return Timeunit.toMini(sourceTime, sourceTimeunit);
    }

    public long toHour() {
        return Timeunit.toHour(sourceTime, sourceTimeunit);
    }

    public long toDay() {
        return Timeunit.toDay(sourceTime, sourceTimeunit);
    }

    public long toWeek() {
        return Timeunit.toWeek(sourceTime, sourceTimeunit);
    }

    public long to28DayMonth() {
        return Timeunit.to28DayMonth(sourceTime, sourceTimeunit);
    }

    public long to29DayMonth() {
        return Timeunit.to29DayMonth(sourceTime, sourceTimeunit);
    }

    public long to30DayMonth() {
        return Timeunit.to30DayMonth(sourceTime, sourceTimeunit);
    }

    public long to31DayMonth() {
        return Timeunit.to31DayMonth(sourceTime, sourceTimeunit);
    }

    public long to365DayYear() {
        return Timeunit.to365DayYear(sourceTime, sourceTimeunit);
    }

    public long to366DayYear() {
        return Timeunit.to366DayYear(sourceTime, sourceTimeunit);
    }

    public String toText() {
        return sourceTime + " " + sourceTimeunit.getText();
    }

    @Override
    public String toString() {
        return "TimePair{" + sourceTime + ' ' + sourceTimeunit + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TimePair timePair = (TimePair) o;
        return sourceTime == timePair.sourceTime && sourceTimeunit == timePair.sourceTimeunit;
    }

    @Override
    public int hashCode() {
        return Objects.hash(sourceTime, sourceTimeunit);
    }



    @Note("解析字符串的过程逻辑")
    private static TimePair private_build(int length, String timeText) {
        StringBuilder number = new StringBuilder();
        StringBuilder unit = new StringBuilder();
        for(int i = 0; i < length; i++) {
            char c = timeText.charAt(i);
            if(Character.isWhitespace(c)) continue;
            if(c >= 48 && c <= 57) {
                number.append(c);
                continue;
            }
            unit.append(c);
        }
        Timeunit timeunit = Timeunit.getEnumByUnit(unit.toString());
        long time = Long.parseLong(number.toString());
        return new TimePair(time, timeunit);
    }
}
